package ATM_mysql;

import java.sql.*;
import java.util.ArrayList;
import java.util.List;

public class MysqlUtil {
    /**
     * 建立一个连接
     */

    public static Connection getConnection() {
        // 1. 加载驱动
        try {
            Class.forName("com.mysql.cj.jdbc.Driver");
            // 2. 创建连接
            Connection con = DriverManager.getConnection(
                    "jdbc:mysql://localhost:3306/lesson3?userSSL=true&useUnicode=true&characterEncoding=UTF8&serverTimezone=GMT",
                    "root", "555430");
            return con;
        } catch (ClassNotFoundException | SQLException e) {
            e.printStackTrace();
        }
        return null;
    }

    /**
     * 关闭连接
     */
    public static void closeConnection(Connection con, Statement statement) {
        if (statement != null) {
            try {
                statement.close();
            } catch (SQLException throwables) {
                throwables.printStackTrace();
            }
        }
        if (con != null) {
            try {
                con.close();
            } catch (SQLException throwables) {
                throwables.printStackTrace();
            }
        }
    }

    /**
     * 关闭连接
     */
    public static void closeConnection(Connection con, Statement statement, ResultSet rs) {
        if (rs != null) {
            try {
                rs.close();
                ;
            } catch (SQLException throwables) {
                throwables.printStackTrace();
            }
            closeConnection(con, statement);
        }
    }

    /**
     * 修改用户数据
     */
    public static void update(User user) {
        //1. 建立连接
        Connection con = getConnection();
        Statement statement = null;
        //2. 获取执行SQL语句执行对象
        try {
            statement = con.createStatement();
            String user_name = user.getAccount();
            String password = user.getPwd();
            double balance = user.getBalance();
            //3. 执行SQL语句
            String update = "update t_user set u_balance=" + balance + " where u_user_name='" + user_name + "' and u_password='" + password + "'";
            int row = statement.executeUpdate(update);
            System.out.println("影响行数：" + row);
        } catch (SQLException throwables) {
            throwables.printStackTrace();
        } finally {
            //关闭连接
            closeConnection(con, statement);
        }
    }

    /**
     * 添加用户数据
     */
    public static void insert(User user) {
        //1. 建立连接
        Connection con = getConnection();
        Statement statement = null;
        //2. 获取执行SQL语句执行对象
        try {
            statement = con.createStatement();
            String user_name = user.getAccount();
            String password = user.getPwd();
            double balance = user.getBalance();
            //3. 执行SQL语句
            String update = "insert into t_user values ('" + user_name + "','" + password + "'," + balance + ")";
            int row = statement.executeUpdate(update);
            System.out.println("影响行数：" + row);
        } catch (SQLException throwables) {
            throwables.printStackTrace();
        } finally {
            //关闭连接
            closeConnection(con, statement);
        }
    }

    /**
     * 查询改据库
     */
    public static List query() {
        //1. 建立连接
        Connection con = getConnection();
        Statement statement = null;
        ResultSet rs = null;
        //2. 获取执行SQL语句执行,并返回结果
        try {
            statement = con.createStatement();
            //3. 执行SQL语句
            String cmd = "select * from t_user";
            rs = statement.executeQuery(cmd);
            List list = new ArrayList();
            while (rs.next()) {
                list.add(new User(rs.getString("u_user_name"),
                        rs.getString("u_password"),
                        rs.getDouble("u_balance")));
            }
            return list;
        } catch (SQLException throwables) {
            throwables.printStackTrace();
        } finally {
            //关闭连接
            closeConnection(con, statement, rs);
        }
        return null;
    }

}
